define("admin/page/manage/edit", [
    "jquery",
    "vue",
    "vue-resource",
    "toastr"
], function($, Vue, VueHttp, toastr){

    Vue.use(VueHttp);

    var vm = new Vue({
        el : '#edit_manager',

        data: {
            name: '',
            email: '',
            status: 1,
            password: '',
            password_confirmation: '',
            token: '',
            pageData:'',
            id:'',
            role:''
        },
        
        methods: {
            submit: function(){
                var self = this;
                if (this.name && this.email) {
                    if (this.password == this.password_confirmation) {
                        this.$http.post("/admin/manage/edit", {
                            'id': this.id,
                            'name': this.name,
                            'email': this.email,
                            'role': this.role,
                            'status': this.status,
                            'password': this.password,
                            '_token': this.token
                        })
                        .then(function(response){
                            return response.json();
                        })
                        .then(function(json){
                            if (json.ret == 0) {
                                toastr.success(json.msg);
                                setTimeout(function(){
                                    window.location.href = '/admin/manage/list';
                                })
                            } else {
                                self.token = json.data.token;
                                toastr.warning(json.msg);
                            }
                        })
                        .catch(function() {
                            toastr.warning('接口请求失败');
                        });
                    } else {
                        toastr.warning('密码两次输入不一致');
                    }
                } else {
                    toastr.warning('请填写完整资料');
                }
            },
            // 获取url中的参数
            getUrlParam: function(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                if (r != null) return unescape(r[2]); return null; //返回参数值
            },
        },

        mounted: function(){
            Vue.set(this, 'token', $("[name='csrf-token']").attr('content'));
            Vue.set(this, 'pageData', PAGE.meta);
            this.id = this.getUrlParam('id');
            this.name = this.pageData.user.name;
            this.email = this.pageData.user.email;
            this.role = this.pageData.user.role ? this.pageData.user.role[0] : '';
            if (this.id) {
                this.status = this.pageData.user.status;
            }
        },
    });
});